<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户"></el-input>
    </el-form-item>
    <el-form-item label="等级类型" prop="levelId">
      <el-input v-model="dataForm.levelId" placeholder="等级类型"></el-input>
    </el-form-item>
    <el-form-item label="证件类型" prop="certificate">
      <el-input v-model="dataForm.certificate" placeholder="证件类型"></el-input>
    </el-form-item>
    <el-form-item label="证件号" prop="certificateNo">
      <el-input v-model="dataForm.certificateNo" placeholder="证件号"></el-input>
    </el-form-item>
    <el-form-item label="名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="名字"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="卡号" prop="cardNo">
      <el-input v-model="dataForm.cardNo" placeholder="卡号"></el-input>
    </el-form-item>
    <el-form-item label="联系地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="商家ID" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="商家ID"></el-input>
    </el-form-item>
    <el-form-item label="推荐员工" prop="salesman">
      <el-input v-model="dataForm.salesman" placeholder="推荐员工"></el-input>
    </el-form-item>
    <el-form-item label="来源" prop="source">
      <el-input v-model="dataForm.source" placeholder="来源"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          levelId: '',
          certificate: '',
          certificateNo: '',
          name: '',
          mobile: '',
          gender: '',
          cardNo: '',
          address: '',
          creator: '',
          createDate: '',
          sellerId: '',
          salesman: '',
          source: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户不能为空', trigger: 'blur' }
          ],
          levelId: [
            { required: true, message: '等级类型不能为空', trigger: 'blur' }
          ],
          certificate: [
            { required: true, message: '证件类型不能为空', trigger: 'blur' }
          ],
          certificateNo: [
            { required: true, message: '证件号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名字不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          cardNo: [
            { required: true, message: '卡号不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '联系地址不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '商家ID不能为空', trigger: 'blur' }
          ],
          salesman: [
            { required: true, message: '推荐员工不能为空', trigger: 'blur' }
          ],
          source: [
            { required: true, message: '来源不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelmembercardrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.tHotelMemberCardRecord.userId
                this.dataForm.levelId = data.tHotelMemberCardRecord.levelId
                this.dataForm.certificate = data.tHotelMemberCardRecord.certificate
                this.dataForm.certificateNo = data.tHotelMemberCardRecord.certificateNo
                this.dataForm.name = data.tHotelMemberCardRecord.name
                this.dataForm.mobile = data.tHotelMemberCardRecord.mobile
                this.dataForm.gender = data.tHotelMemberCardRecord.gender
                this.dataForm.cardNo = data.tHotelMemberCardRecord.cardNo
                this.dataForm.address = data.tHotelMemberCardRecord.address
                this.dataForm.creator = data.tHotelMemberCardRecord.creator
                this.dataForm.createDate = data.tHotelMemberCardRecord.createDate
                this.dataForm.sellerId = data.tHotelMemberCardRecord.sellerId
                this.dataForm.salesman = data.tHotelMemberCardRecord.salesman
                this.dataForm.source = data.tHotelMemberCardRecord.source
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelmembercardrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'levelId': this.dataForm.levelId,
                'certificate': this.dataForm.certificate,
                'certificateNo': this.dataForm.certificateNo,
                'name': this.dataForm.name,
                'mobile': this.dataForm.mobile,
                'gender': this.dataForm.gender,
                'cardNo': this.dataForm.cardNo,
                'address': this.dataForm.address,
                'creator': this.dataForm.creator,
                'createDate': this.dataForm.createDate,
                'sellerId': this.dataForm.sellerId,
                'salesman': this.dataForm.salesman,
                'source': this.dataForm.source
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
